import config from '@/admin.config';

// 路由拦截器
const routerInterceptor = {
	routeChange() {
		uni.$emit('route-change');
	},
};

export function initInterceptor() {
	// 监听页面显示
	uni.addInterceptor('navigateTo', {
		success: routerInterceptor.routeChange,
		fail: (error) => {
			const { errMsg } = error;
			if (errMsg.indexOf('is not found') !== -1) {
				// 404
				uni.navigateTo({
					url: `${config.error['404']}?errMsg=${errMsg}`,
				});
			}
		},
	});

	uni.addInterceptor('redirectTo', {
		success: routerInterceptor.routeChange,
	});

	uni.addInterceptor('reLaunch', {
		success: routerInterceptor.routeChange,
	});

	uni.addInterceptor('switchTab', {
		success: routerInterceptor.routeChange,
	});
}
